package com.xmim.fulcrum.factory;

import com.xmim.core.session.DefaultSessionFactory;
import com.xmim.core.session.SessionManager;
import com.xmim.core.session.user.DefaultUserSessionManager;
import com.xmim.core.session.user.UserSessionManager;
import com.xmim.fulcrum.*;
import com.xmim.fulcrum.codec.JDKCodec;
import com.xmim.fulcrum.session.FulcrumSessionManager;
import com.xmim.fulcrum.session.UserSessionsDao;

public class DefaultFulcrumFactory extends DefaultSessionFactory<UserSessionManager> implements FulcrumFactory {

    private FulcrumConfig config;
    private FulcrumCodec codec;
    private UserSessionsDao userSessionsDao;


    public DefaultFulcrumFactory(FulcrumConfig config) {
        this.config = config;
        this.sessionManager = new DefaultUserSessionManager();
        this.codec = new JDKCodec();
    }


    public void setCodec(FulcrumCodec codec) {
        this.codec = codec;
    }

    public void setUserSessionsDao(UserSessionsDao userSessionsDao) {
        this.userSessionsDao = userSessionsDao;
    }


    @Override
    public FulcrumCodec getCodec() {
        return codec;
    }

    @Override
    public UserSessionsDao getUserSessionsDao() {
        return userSessionsDao;
    }

    @Override
    public FulcrumConfig getConfig() {
        return config;
    }

    @Override
    public long getIdByPath(String path) {
        String sid = null;
        if (null == path) {
            throw new RuntimeException("节点路径有误");
        }
        int index = path.lastIndexOf(this.config.getSeqPrefixPath());
        if (index >= 0) {
            index += this.config.getSeqPrefixPath().length();
            sid = index <= path.length() ? path.substring(index) : null;
        }
        if (null == sid) {
            throw new RuntimeException("节点ID获取失败");
        }
        return Long.parseLong(sid);
    }


}
